﻿using Client;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageSystem.Dialog
{
    public partial class UpdateOrInsertWorker : Form
    {
        private bool _isUpdate = false;
        private WORKER _choiceWorker = null;
        private List<WORKER> _listWorker = null;

        public UpdateOrInsertWorker(List<WORKER> list,WORKER worker=null,bool isUpdate=false)
        {
            _isUpdate = isUpdate;
            _choiceWorker = worker;
            _listWorker = list;
            InitializeComponent();
        }

        //加载信息到界面
        private void SetMessage(WORKER worker)
        {
            if (worker == null) return;

            textBoxName.Text = worker.NAME;
            comboBoxPartment.Text = worker.PARTMENT;
            textBoxPhone.Text = worker.PHONE;
            comboBoxSex.Text = worker.SEX;
            textBoxEDUCATION.Text = worker.EDUCATION;
            textBoxMarriage.Text = worker.MARRIAGE;
            textBoxCardId.Text = worker.CARDID;
            textBoxNATION.Text = worker.NATION;
            textBoxSchool.Text = worker.SCHOOL;
            textBoxPLACE_ORIGIN.Text = worker.PLACE_ORIGIN;
            textBoxHUKOU.Text = worker.HUKOU;
            textBoxADDRESS.Text = worker.ADDRESS;
            textBoxINTRODUCE.Text = worker.INTRODUCE;
            textBoxCONTEXT.Text = worker.CONTEXT;
            if (worker.FLAG != null && worker.FLAG.Value)
                labelOnWork.Text = "在职";
            else
                labelOnWork.Text = "离职";

            textBoxEMERGENCY_CALL.Text = worker.EMERGENCY_CALL;
            textBoxMAJOR.Text = worker.MAJOR;

            if (worker.BIRTHDAY != null)
                dateTimePickerBirthday.Value = worker.BIRTHDAY.Value;
            if (worker.ENTER_DATE != null)
                dateTimePickerEnterDate.Value = worker.ENTER_DATE.Value;
            if (worker.OUT_DATE != null)
                dateTimePickerOUT_DATE.Text = worker.OUT_DATE.Value.ToString() ;
            if (worker.HETONG_START != null)
                dateTimePickerHETONG_START.Value = worker.HETONG_START.Value;
            if (worker.HETONG_END != null)
                dateTimePickerHETONG_END.Value = worker.HETONG_END.Value;
        }

        //清空界面信息
        private void ClearMessage()
        {
            textBoxName.Clear();
            comboBoxPartment.Text = null;
            textBoxPhone.Clear();
            comboBoxSex.Text = "保密";
            textBoxEDUCATION.Text = "";
            textBoxMarriage.Text = "";
            textBoxCardId.Clear();
            textBoxNATION.Clear();
            textBoxSchool.Clear();
            textBoxPLACE_ORIGIN.Clear();
            textBoxHUKOU.Clear();
            textBoxADDRESS.Clear();
            textBoxINTRODUCE.Clear();
            textBoxCONTEXT.Clear();
            labelOnWork.Text = "在职";
            textBoxEMERGENCY_CALL.Clear();
            textBoxMAJOR.Clear();
            //dateTimePickerBirthday
            // dateTimePickerEnterDate;
            //dateTimePickerOUT_DATE;
            //dateTimePickerHETONG_START;
            //dateTimePickerHETONG_END;
            //dateTimePickerLiveDate;
        }
        //加载员工信息
        private void UpdateOrInsertWorker_Load(object sender, EventArgs e)
        {
            //没有选择员工信息
            if (_isUpdate&&(_listWorker.Count==0||_choiceWorker==null))
            {
                MessageBox.Show("没有传入员工信息");
                Close();
                return;
            }

            //加载部门信息
            if (_listWorker.Count >= 1)
            {
                this.comboBoxPartment.Items.Clear();
                try
                {
                    var parts = (from w in _listWorker
                                 select w.PARTMENT).Distinct().ToList();

                    for (int i = 0; parts != null && i < parts.Count; i++)
                    {
                        //获取的部门信息不为空
                        this.comboBoxPartment.Items.Add(parts[i]);
                    }
                }
                catch { }//用于吞掉异常
            }

            if (_isUpdate)
            {
                //修改员工信息，则将员工信息加载到界面上
                SetMessage(_choiceWorker);
                //隐藏增加按钮
                this.buttonAdd.Visible = false;
                if (_choiceWorker.FLAG == false)
                {
                    this.buttonLive.Visible = false;
                    this.dateTimePickerLiveDate.Visible = false;
                }
            }
            else
            {
                //清空界面
                ClearMessage();
                //创建一个员工类
                _choiceWorker = new WORKER();
                //隐藏修改按钮/离职按钮和离职时间和在职状态条
                this.buttonChg.Visible = false;
                this.buttonLive.Visible = false;
                this.dateTimePickerLiveDate.Visible = false;
                this.labelOnWork.Visible = false;
            }

            _choiceWorker.SetUpdate();//设置更新，让所有做出的更改生效
        }
        //离职
        private void buttonLive_Click(object sender, EventArgs e)
        {
            if (_isUpdate == false||_choiceWorker==null) return;//为更新状态时有效

            DateTime live = this.dateTimePickerLiveDate.Value;

            var res = MessageBox.Show(string.Format("离职时间为：{0}\n确定离职？", live), "警告"
                , MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (res == DialogResult.No) return;//选择不离职返回操作界面

            //更新离职标志/增加离职时间
            _choiceWorker.Clear();
            _choiceWorker.SetWhere();
            _choiceWorker.ID = _choiceWorker.ID;
            _choiceWorker.SetUpdate();
            _choiceWorker.FLAG = false;
            _choiceWorker.OUT_DATE = live;

            var updateWorker = MethodWcf.UpdateWorkerClient(_choiceWorker);

            MessageBox.Show(updateWorker.message);
            Close();
        }

        //修改
        private void buttonChg_Click(object sender, EventArgs e)
        {
            _choiceWorker.SetWhere();
            _choiceWorker.ID = _choiceWorker.ID;

            var updateWorker = MethodWcf.UpdateWorkerClient(_choiceWorker);
            MessageBox.Show(updateWorker.message);
            Close();
        }

        //添加
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            var insertWorker = MethodWcf.InsertWorkerClient(_choiceWorker);
            if (insertWorker.result == false||insertWorker.message=="0")
            {
                MessageBox.Show(string.Format("增加失败，{0}",insertWorker.message));
            }
            else
            {
                MessageBox.Show(string.Format("增加成功，员工ID为：{0}", insertWorker.message));
            }
            Close();
        }
        //姓名
        private void textBoxName_Leave(object sender, EventArgs e)
        {
            string name = this.textBoxName.Text.Trim();
            if (_choiceWorker != null&&string.IsNullOrEmpty(name)==false)
            {
                _choiceWorker.NAME = name;
            }
        }
        //部门
        private void comboBoxPartment_Leave(object sender, EventArgs e)
        {
            string part = this.comboBoxPartment.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(part) == false)
            {
                _choiceWorker.PARTMENT = part;
            }
        }
        //手机号
        private void textBoxPhone_Leave(object sender, EventArgs e)
        {
            string phone = this.textBoxPhone.Text.Trim();

            if (phone.Length != 11)
            {
                MessageBox.Show("手机号码必须为11位的数字");
                return;
            }

            if (_choiceWorker != null && string.IsNullOrEmpty(phone) == false)
            {
                _choiceWorker.PHONE = phone;
            }
        }
        //性别
        private void comboBoxSex_Leave(object sender, EventArgs e)
        {
            string sex = this.comboBoxSex.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(sex) == false)
            {
                _choiceWorker.SEX = sex;
            }
        }
        //出生日期
        private void dateTimePickerBirthday_Leave(object sender, EventArgs e)
        {
            DateTime birthday = this.dateTimePickerBirthday.Value;
            if (_choiceWorker != null )
            {
                _choiceWorker.BIRTHDAY = birthday;
            }
        }
        //身份证
        private void textBoxCardId_Leave(object sender, EventArgs e)
        {
            string cardId = this.textBoxCardId.Text.Trim();
            if (cardId.Length != 18)
            {
                MessageBox.Show("身份证号码长度位18位");
                return;
            }
            if (_choiceWorker != null && string.IsNullOrEmpty(cardId) == false)
            {
                _choiceWorker.CARDID = cardId;
            }
        }
        //入职日期
        private void dateTimePickerEnterDate_Leave(object sender, EventArgs e)
        {
            DateTime enter = this.dateTimePickerEnterDate.Value;
            if (_choiceWorker != null)
            {
                _choiceWorker.ENTER_DATE = enter;
            }
        }
        //紧急电话
        private void textBoxEMERGENCY_CALL_Leave(object sender, EventArgs e)
        {
            string call = this.textBoxEMERGENCY_CALL.Text.Trim();

            if (call.Length != 11)
            {
                MessageBox.Show("手机号码必须为11位的数字");
                return;
            }

            if (_choiceWorker != null && string.IsNullOrEmpty(call) == false)
            {
                _choiceWorker.EMERGENCY_CALL = call;
            }
        }
        //籍贯
        private void textBoxPLACE_ORIGIN_Leave(object sender, EventArgs e)
        {
            string origin = this.textBoxPLACE_ORIGIN.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(origin) == false)
            {
                _choiceWorker.PLACE_ORIGIN = origin;
            }
        }
        //户口
        private void textBoxHUKOU_Leave(object sender, EventArgs e)
        {
            string hk = this.textBoxHUKOU.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(hk) == false)
            {
                _choiceWorker.HUKOU = hk;
            }
        }
        //婚姻状况
        private void textBoxMarriage_Leave(object sender, EventArgs e)
        {
            string marr = this.textBoxMarriage.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(marr) == false)
            {
                _choiceWorker.MARRIAGE = marr;
            }
        }
        //学历
        private void textBoxEDUCATION_Leave(object sender, EventArgs e)
        {
            string xl = this.textBoxEDUCATION.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(xl) == false)
            {
                _choiceWorker.EDUCATION = xl;
            }

        }
        //学校
        private void textBoxSchool_Leave(object sender, EventArgs e)
        {
            string school = this.textBoxSchool.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(school) == false)
            {
                _choiceWorker.SCHOOL = school;
            }
        }
        //专业
        private void textBoxMAJOR_Leave(object sender, EventArgs e)
        {
            string major = this.textBoxMAJOR.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(major) == false)
            {
                _choiceWorker.MAJOR = major;
            }
        }
        //民族
        private void textBoxNATION_Leave(object sender, EventArgs e)
        {
            string nation = this.textBoxNATION.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(nation) == false)
            {
                _choiceWorker.NATION = nation;
            }
        }
        //介绍人
        private void textBoxINTRODUCE_Leave(object sender, EventArgs e)
        {
            string introduce = this.textBoxINTRODUCE.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(introduce) == false)
            {
                _choiceWorker.INTRODUCE = introduce;
            }
        }
        //合同生效日期
        private void dateTimePickerHETONG_START_Leave(object sender, EventArgs e)
        {
            DateTime start = this.dateTimePickerHETONG_START.Value;
            if (_choiceWorker != null)
            {
                _choiceWorker.HETONG_START = start;
            }
        }
        //合同结束日期
        private void dateTimePickerHETONG_END_Leave(object sender, EventArgs e)
        {
            DateTime end = this.dateTimePickerHETONG_END.Value;
            if (_choiceWorker != null)
            {
                _choiceWorker.HETONG_END = end;
            }
        }
        //地址
        private void textBoxADDRESS_Leave(object sender, EventArgs e)
        {
            string addr = this.textBoxADDRESS.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(addr) == false)
            {
                _choiceWorker.ADDRESS = addr;
            }
        }
        //描述
        private void textBoxCONTEXT_Leave(object sender, EventArgs e)
        {
            string cont = this.textBoxCONTEXT.Text.Trim();
            if (_choiceWorker != null && string.IsNullOrEmpty(cont) == false)
            {
                _choiceWorker.CONTEXT = cont;
            }
        }
    }
}
